package com.company.cloud.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.web.service.WebArtService;
import com.company.cloud.web.service.WebIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * 新闻资讯
 */
@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private WebArtService webArtService;

    @Autowired
    private WebIndexService webIndexService;

    /**
     * 文章列表
     */
    @RequestMapping("/index")
    public String query(Model model, @RequestParam Map<String, String> params,
                        @CookieValue(value = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE", defaultValue = "zh-CN") String i18n) {
        System.out.println("++++++++++++ CompanyController:query");
        JSONObject data = webArtService.newsQuery(params);
        model.addAttribute("data", data);
        Map<String, String> params2 = new HashMap<>();
        params2.put("categoryId", "5");
        JSONObject list = webArtService.newsQuery(params2);
        model.addAttribute("list", list);
        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("category", indexMap.get("category"));
        Map<String, Object> menusMap = webArtService.staticPageTree("top");
        model.addAttribute("menu", menusMap);
        model.addAttribute("i18n", i18n);
        Map<String, Object> footer = webArtService.staticPageTree("bottom");
        model.addAttribute("footer", footer);
        model.addAttribute("copyright", indexMap.get("copyright"));
        model.addAttribute("friendLink", indexMap.get("friendLink"));
        final JSONObject jsonObject = webArtService.CategoryTree("PTC");
        List<JSONObject> list1 = new ArrayList<>();
        JSONObject categoryList = new JSONObject();
        final JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (jsonArray != null) {
            int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                if (item.getString("type").equals("2")) {
                    list1.add(item);
                }
            }
        }
        categoryList.put("data", list1);
        model.addAttribute("categoryList", categoryList);
        return "news/index";
    }

    /**
     * 文章详情
     */
    @RequestMapping("/detail")
    public String detail(Model model, String newsNo, @CookieValue(value = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE", defaultValue = "zh-CN") String i18n) {
        System.out.println("++++++++++++ NewsController.detail");
        JSONObject data = webArtService.newsInfo(newsNo);
        model.addAttribute("data", data);
        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("category", indexMap.get("category"));
        Map<String, String> params2 = new HashMap<>();
        params2.put("categoryId", "5");
        JSONObject list = webArtService.newsQuery(params2);
        model.addAttribute("list", list);
        Map<String, Object> menusMap = webArtService.staticPageTree("top");
        model.addAttribute("menu", menusMap);
        model.addAttribute("i18n", i18n);
        Map<String, Object> footer = webArtService.staticPageTree("bottom");
        model.addAttribute("footer", footer);

        model.addAttribute("friendLink", indexMap.get("friendLink"));
        model.addAttribute("copyright", indexMap.get("copyright"));

        final JSONObject jsonObject = webArtService.CategoryTree("PTC");
        List<JSONObject> list1 = new ArrayList<>();
        JSONObject categoryList = new JSONObject();
        final JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (jsonArray != null) {
            int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                if (item.getString("type").equals("2")) {
                    list1.add(item);
                }
            }
        }
        categoryList.put("data", list1);
        model.addAttribute("categoryList", categoryList);

        return "news/detail";
    }

}
